<?php

    class LoginView {
      
	  	private $_usernameInput = "usernameInput";
		private $_passwordInput = "passwordInput";
		private $_loginInput = "loginInput";
		private $_logoutInput = "logoutInput";
		private $_checkRemember = "checkRemember";

		// Function used to create the Login form.
		// Returns XHTML string
        public function DoLoginBox(){
        	
			return "
	<form method='get'>
		Username: <input type='text'  maxlength='20' name=$this->_usernameInput></input>
		Password: <input type='password'  maxlength='20' name=$this->_passwordInput ></input>
		<input type='submit' name=$this->_loginInput value='Login'/>
		Remember me: <input type='checkbox' name=$this->_checkRemember />
	</form>";
        }
		
		// Function used to create the Logout button.
		// Returns XHTML string
		public function DoLogoutBox(){
				
			return "<form method='get'><input type='submit' name=$this->_logoutInput value='Logout'/></form>";
		}
		
		// Function used to retrive a submitted username.
		// Returns a username if existing, otherwise null.
		public function GetUserName(){
			if (isset( $_COOKIE[$this->_usernameInput])){
				$_GET[$this->_checkRemember] = TRUE;
				return $_COOKIE[$this->_usernameInput];
			}
			else if (isset( $_GET[$this->_usernameInput])) {
				return $_GET[$this->_usernameInput];
			}
			return null;
		}
		
		// Function used to retrive a submitted password.
		// Returns a password if existing, otherwise null.
		public function GetPassword(){
			if (isset( $_COOKIE[$this->_passwordInput])){
				return $_COOKIE[$this->_passwordInput];
			}
			else if (isset( $_GET[$this->_passwordInput])) {
				if (Count($_GET[$this->_passwordInput]) == 32){
					return null;
				}
				else { 
					return $_GET[$this->_passwordInput];
				}
				
			}
			return null;
		}
		
		// Function used to see if the login button have been pressed.
		// Returns true if the button have been pressed, otherwise false.
		public function TriedToLogIn(){
			if (isset($_COOKIE[$this->_usernameInput]))
			{
				return true;
			}
			else {
				if (isset( $_GET[$this->_loginInput]) == true) {
					return true;
				}
			}
			return false;
		}
		
		// Function used to see if the logout button have been pressed.
		// Returns true if the button have been pressed, otherwise false.
		public function TriedToLogOut(){
			if (isset( $_GET[$this->_logoutInput]) == true) {
				return true;
			}
			return false;
		}
		
		public function ISRememberBox(){
			if (isset($_GET[$this->_checkRemember])){
				return true;
			}
			return false;
		}
		
		public function SetUsernameCookie(){
			setcookie($this->_usernameInput, $this->GetUserName(), time()+60*60*24*10);
		}
		
		public function SetPasswordCookie($password){
			setcookie($this->_passwordInput, $password, time()+60*60*24*10);
		}

		public function RemoveLoginCookies(){
			setcookie($this->_usernameInput, '', time()-60*60*24*365);
			setcookie($this->_passwordInput, '', time()-60*60*24*365);
		}
		
		public function AddMessageLogout(){
			return "You have been successfully logged out";
		}
		
		public function AddMessageWelcome(){
			$Username = $this->GetUserName();
			if(isset($_COOKIE[$this->_usernameInput])){
				return "Welcome back,  $Username !";
			}
			return "Welcome, $Username !";				
		}
		
		public function AddMessageIncorrectInfo(){
			return "Incorrect Username or Password";
		}
		
		public function AddHeader(){
			return "<h3>LOGIN:</h3>";
		}
		
		public function AddMessageLoginStatus($isTheUserLoggedIn, $username){
			if($isTheUserLoggedIn == true){
				return "You are logged in as $username";
			}else{
				return "You are not logged in";
			}
				
		}
	}   

     